package cn.caskm.souvenirshopsystem.controller.api;

import cn.caskm.souvenirshopsystem.domain.Result;
import cn.caskm.souvenirshopsystem.domain.dto.PageDTO;
import cn.caskm.souvenirshopsystem.domain.po.GoodsOrder;
import cn.caskm.souvenirshopsystem.domain.query.MyOrderQuery;
import cn.caskm.souvenirshopsystem.domain.vo.MyOrderVO;
import cn.caskm.souvenirshopsystem.service.GoodsOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {

    private final GoodsOrderService orderService;

    /**
     * 支付的时候直接提交订单，如果goodsId为空则代表在购物车界面购买，否则就是在商品详情直接购买
     * @param goodsId 商品id
     * @param goodsNum 购买数量
     * @param userCouponId 用户优惠券id
     * @return 订单ID
     */
    @PostMapping("/submitOrder")
    public Result<Integer> submitOrder(@RequestParam(value = "goodsId", required = false) Integer goodsId,
                                      @RequestParam(value = "goodsNum", required = false) Integer goodsNum,
                                      @RequestParam(value = "userCouponId", required = false) Long userCouponId) {
        Integer orderId = orderService.submitOrder(goodsId, goodsNum, userCouponId);
        return Result.success(orderId);
    }

    @GetMapping("/listMyOrder")
    public Result<PageDTO<MyOrderVO>> listMyOrder(MyOrderQuery query) {
        return Result.success(orderService.listMyOrder(query));
    }

    @PutMapping("/updateOrder")
    public Result<Object> updateOrder(@RequestBody GoodsOrder order) {
        orderService.updateById(order);
        return Result.success();
    }

}
